$(function () {
    obj.init();
});

window.obj = new function() {
    return {
        dataGrid: null,
        form: null,
        params: {
        },
        dom: {
            target: null,
            schoolId: null,
            userType: null,
            className: null,
            depId: null
        },
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'Ask/getSurvey');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_del', 'bt_list_enable', 'bt_list_disable'], ['bt_list_question', 'bt_list_statistics']);
            xhExtra.clearLoadSuccess(this.dataGrid);

            this.form = new xhForm($('#xh_form'));
            this.form.parse().bindRequired($('#bt_form_add,#bt_form_edit'));

            this.dom.target   = $('#iTarget');
            this.dom.schoolId = $('#sSchoolId');
            this.dom.userType = $('#sUserType');
            this.dom.className = $('#sClassName');
            this.dom.depId    = $('#sDepId');

            this.dom.schoolId.select2({placeholder: '学校', allowClear: true, multiple: true});
            this.dom.userType.select2({placeholder: '角色', allowClear: true, multiple: true});
            this.dom.className.select2({placeholder: '班级(仅学生)', allowClear: true, multiple: true});
            this.dom.depId.combotree({
                url: baseUrl + 'other/getDep',
                multiple: true,
                cascadeCheck: false
            });

            this.listener();
        },
        listener: function () {
            var $this = this;
            //基础绑定
            xhExtra.bindFormBack();
            xhExtra.bindFormReset();
            xhExtra.bindListAdd($this.form);
            xhExtra.bindListDel($this.dataGrid, 'iASId', 'Ask/delSurvey');
            //新增问卷打开
            $('#bt_list_add').click(function(){
                $this.funObj().newSurvey();
            });
            //保存问卷
            $('#bt_form_add').click(function () {
                $this.funObj().save();
            });
            //添加题目
            $('#bt_list_question').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().openQuestion();
            });
            //查看统计
            $('#bt_list_statistics').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().openStatistic();
            });
            //启用
            $('#bt_list_enable').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().setStatus(0);
            });
            //禁用
            $('#bt_list_disable').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().setStatus(1);
            });
            //对象切换
            $this.dom.target.change(function () {
                $this.funObj().changeTarget($(this).val());
            }).trigger('change');
            //学校单选时提供班级选择
            $this.dom.schoolId.on('change', function () {
                $this.funObj().changeSchool();
            });
            //角色选择，关联班级
            $this.dom.userType.on('change', function () {
                $this.funObj().changeSchool();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                save: function () {
                    var schoolId  = $this.dom.schoolId.val(),
                        userType  = $this.dom.userType.val(),
                        className = $this.dom.className.val(),
                        depId     = $this.dom.depId.combotree('getValues');
                    schoolId  = schoolId ? schoolId.join(',') : '';
                    userType  = userType ? userType.join(',') : '';
                    className = className ? className.join(',') : '';
                    depId     = depId ? depId.join(',') : '';
                    $this.form.submit(
                        baseUrl + 'Ask/saveSurvey',
                        {sSchoolId: schoolId, sUserType: userType, sDepId: depId, sClassName: className},
                        function (data) {
                            if (xhExtra.checkError(data)) return false;
                            xhExtra.backToList($this.dataGrid);
                        }
                    );
                },
                newSurvey: function () {
                    $this.form.reset();
                    xhExtra.forwardToForm(false);
                    $this.dom.schoolId.add($this.dom.userType).val(null).trigger('change');
                    // $this.dom.userType.val(null).trigger('change');
                    $this.dom.depId.combotree('clear')
                },
                openQuestion: function () {
                    var id = $this.dataGrid.getSelectValue('iASId');
                    window.top.xhTop.showModal({
                        title: '选择题目',
                        content: xhTool.getFrameString('Ask/surveyQuestion', {id: id}, 500),
                        size: 'lg',
                        button: '确认'
                    });
                },
                openStatistic: function () {
                    var id = $this.dataGrid.getSelectValue('iASId');
                    window.top.xhTop.showModal({
                        title: '查看统计',
                        content: xhTool.getFrameString('Ask/askSurveyStatistics', {iASId: id}, 600),
                        size: 'lg'
                    });
                },
                setStatus: function (status) {
                    var ids = $this.dataGrid.getSelectValues('iASId');
                    xhExtra.askChangeStatus('Ask/setSurveyStatus', {ids: ids, status: status}, $this.dataGrid, '确认' + (status ? '禁用' : '启用') + '?');
                },
                changeTarget: function (target) {
                    if (target === '1') {
                        $this.dom.depId.combotree('clear');
                        $this.dom.depId.parents('tr:eq(0)').hide();
                        $this.dom.schoolId.parents('tr:eq(0)').slideDown();
                        $this.dom.className.parents('tr:eq(0)').slideDown();
                    } else {
                        $this.dom.schoolId.add($this.dom.userType).val(null).trigger('change');
                        $this.dom.schoolId.parents('tr:eq(0)').hide();
                        $this.dom.className.parents('tr:eq(0)').hide();
                        $this.dom.depId.parents('tr:eq(0)').slideDown();
                    }
                },
                changeSchool: function () {
                    var schoolId = $this.dom.schoolId.val();
                    var userType = $this.dom.userType.val();
                    if (!schoolId || schoolId.length !== 1 || !userType || userType.length !== 1 || parseInt(userType[0]) !== 1) {
                        $this.dom.className.empty().select2({placeholder: '班级(仅学生)'});
                        return false;
                    }
                    xhExtra.ajaxRequest('ask/getSchoolAdClass', {schoolId: schoolId[0]}, {check: true}, function (classNames) {
                        if (classNames.length < 1) {
                            $this.dom.className.empty().select2({placeholder: '班级(仅学生)'});
                            return false;
                        }
                        var data = [];
                        $.each(classNames, function (index, className) {
                            data.push({id: className, text: className});
                        });
                        $this.dom.className.select2({data: data, placeholder: '选择班级'});
                    });
                }
            }
        }
    }
};

/**
 * Created by CK on 2016/7/26.
 */
// $(document).ready(function(){
//     var list = $('#List');
//     $('.xh-datagrid-toolbar').parseToolbar();
//     $('.xh-form').parseForm($('#bt_form_add, #bt_form_edit'));
//     list.initDataGrid('getSurveyDgData')
//         .setSelectThenEnable(['bt_list_del', 'bt_list_enable', 'bt_list_disable'], ['bt_list_Statistics']);
//     /** 列表 **/
//     $('#bt_list_del').click(function () {
//         var ids = list.getDataGridSelectedFields('iASId');
//         $.overtFun.clickDelList(ids,'delSurveyData');
//     });
//     $('#bt_list_enable').click(function(){
//         if($(this).checkButtonDisabled()) return false;
//         var ids = list.getDataGridSelectedFields('iASId');
//         $.overtFun.setModelState('setSurveyState', ids,'enable');
//     });
//     $('#bt_list_disable').click(function(){
//         if($(this).checkButtonDisabled()) return false;
//         var ids = list.getDataGridSelectedFields('iASId');
//         $.overtFun.setModelState('setSurveyState', ids,'disable');
//     });
//     $('#bt_list_Statistics').click(function(){
//         var id = list.getDataGridSelectedField('iASId');
//         //window.location.href = baseUrl + 'Ask/askSurveyStatistics?iASId=' + id;
//         var src = baseUrl + 'Ask/askSurveyStatistics?iASId=' + id;
//         window.top.xhTop.showModal({
//             title: '查看统计',
//             content: '<iframe frameborder="0" style="width: 100%;height:600px;" src="'+ src +'"></iframe>',
//             size: 'lg'
//         });
//     });
// });